package com.api.gbhg.huangwm.action;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.huangwm.dao.ProjectOperationDao;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-04-25
 * @Version 1.0
 * @Description TODO 项目使用单位修改流程附加操作
 **/
public class ProjectUseUnitModify implements Action {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/ProjectUseUnitModify",false);

    private ProjectOperationDao dao = new ProjectOperationDao();
    @Override
    public String execute(RequestInfo requestInfo) {
        User user = requestInfo.getRequestManager().getUser();
        logTool.writeLog("项目使用单位修改 当前操作人"+user.getUID());
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        Map<String,String> data = new HashMap<>();
        String table = "formtable_main_971";
        RecordSet rs = new RecordSet();
        rs.execute("select * from "+table+" where requestid="+979979);
        rs.next();
        for (Property property : properties){
            data.put(property.getName(),property.getValue());
        }
        String xmlcid = data.getOrDefault("dzysxm","");
        String u1 = data.getOrDefault("dzqsydw","");
        String u2 = data.getOrDefault("dzhsydw","");
        Map<String,Object> right = dao.hasRight(user,xmlcid);
        logTool.writeLog(JSON.toJSONString(right));
        boolean apiStatus = (boolean) right.get("apiStatus");
        boolean hasRight = (boolean) right.get("hasRight");
        if(!apiStatus || !hasRight){
            requestInfo.getRequestManager().setMessageid(xmlcid);
            requestInfo.getRequestManager().setMessagecontent(right.get("msg").toString());
            return FAILURE_AND_CONTINUE;
        }
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try{
            rst.execute(" update uf_jingfei_version set sydw='"+u2+"' where lcjlsqjlid="+xmlcid);
            rst.execute(" update uf_jingfei set sydw='"+u2+"' where lcjlid="+xmlcid);
            rst.execute("update "+dao.getWorkflowTabNameByRequestid(rs,xmlcid)+" set sydw='"+u2+"' where requestid="+xmlcid);
        }catch (Exception e){
            rst.rollback();
            requestInfo.getRequestManager().setMessageid(xmlcid);
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return SUCCESS;
    }
}
